<?php

namespace frontend\controllers;
use Yii;
use frontend\models\User;
use frontend\models\Stb_area;
use backend\models\Position;
use backend\models\Classify;
use yii\web\Session;
use frontend\models\User_details;
use frontend\models\Advertiser_info;
use frontend\models\UserPosition;
class HomeController extends \yii\web\Controller
{
    public $layout = false;
    public function actionIndex()
    {
        $session = Yii::$app->session;
        $user_id = $session['user']['user_id'];
        $stb_area = new Stb_area;
        $position = new Position;
            $classify_id = Classify::find()->where("classify_action!=0")->all();
            $classifys = Classify::find()->all();
            $posi=$position->find()->where("pos_show='1'")->all();
            // echo "<pre>";
            // print_r($posi);die;
            foreach($posi as $k =>$v){
                foreach($classify_id as $a=>$s){
                    if($v['classify_id']==$s['id']){
                        $p[$k]['classify_name'] = $s['classify_name'];
                    }
                }
            }
            foreach($classifys as $k=>$v){
                    for($a=0;$a<count($classifys);$a++){
                        if($v['classify_action']==$classifys[$a]['id']){
                            $asp[]['classify_name']=$v['classify_name'];
                        }
                    }
                // break;
            } 
            // print_r($asp);
            // die();
        $data = Yii::$app->request->get();
            unset($data['w']);
        if($data==array()){
                        $area_name = '北京';
                        $posi=$position->find()->where("pos_show='1' and pos_area like '%$area_name%'")->all();
                        $stb=$stb_area->find()->where("area_name = '$area_name'")->one();
            $das=$stb_area->find()->where("level=1")->all();
            $da = $stb_area->find()->where("parent_id=".$stb['id']."")->all();
            return $this->render('index',['stb'=>$da,'popo'=>$posi,'classify_name'=>$p,'das'=>$das,'asp'=>$asp]);
        }else{
            // print_r($data);die;
                        $area_name = Yii::$app->request->get('area_name');
                        $settlement = Yii::$app->request->get('settlement');
                        $pos_sex = Yii::$app->request->get('pos_sex');
                        if($settlement==0||$settlement==''){
                                    if($pos_sex==''||$pos_sex==0){
                                                $posi=$position->find()->where("pos_show='1' and pos_area like '%$area_name%'")->all();
                                    }else{
                                                $posi=$position->find()->where("pos_sex in ('$pos_sex','0') and pos_show='1' and pos_area like '%$area_name%'")->all();
                                    }
                        }else{
                                    if($pos_sex==''||$pos_sex==0){
                                                $posi=$position->find()->where(" settlement = '$settlement' and pos_show='1' and pos_area like '%$area_name%'")->all();
                                    }else{
                                                $posi=$position->find()->where(" pos_sex in ('$pos_sex','0') and settlement = '$settlement' and pos_show='1' and pos_area like '%$area_name%'")->all();
                                    }
                        }
                        $stb=$stb_area->find()->where("area_name = '$area_name'")->one();
                        $das=$stb_area->find()->where("level=1")->all();
            if($data['level']==1){
                $da = $stb_area->find()->where("parent_id=".$data['id']."")->all();
                return $this->render('index',['stb'=>$da,'popo'=>$posi,'classify_name'=>$p,'das'=>$das,'asp'=>$asp]);
            }
            else if($data['level']==2){
                $da = $stb_area->find()->where("parent_id=".$data['id']."")->all();
                return $this->render('index',['stb'=>$da,'popo'=>$posi,'classify_name'=>$p,'das'=>$das,'asp'=>$asp]);
            }else if($data['level']==3){
                // print_r($data);die;
                $da = $stb_area->find()->where("parent_id=".$data['parent_id']."")->all();
                return $this->render('index',['stb'=>$da,'popo'=>$posi,'classify_name'=>$p,'das'=>$das,'asp'=>$asp]);
            }
        }
    }
    //个人信息管理
    public function actionUser_info()
    {
        header('content-type:text/html;charset=utf8');
        $session = new Session();
        if (empty($session['user'])) {
            echo "<script>if(confirm('您还未登录是否登录?')){location.href='?w=login/login';}else{location.href='?w=home/index';}</script>";die;
        }
        $user_id = $session['user']['user_id'];
        $user_info = User_details::find()->where(['user_id'=>$user_id])->asArray()->one();
        return $this->render('User_info',['user'=>$user_info]);
    }
    public function actionUp_user(){
        header('content-type:text/html;charset=utf8');
        $session = new Session();
        if (empty($session['user'])) {
            echo "<script>if(confirm('您还未登录是否登录?')){location.href='?w=login/login';}else{location.href='?w=home/index';}</script>";die;
        }
        $data = Yii::$app->request->post();
        $model = new User_details();
        if($model->update_User($data,$session['user']['user_id'])){
            echo "<script>location.href='?w=home/user_info';</script>";die;
        }else{
            echo "<script>if(confirm('修改失败')){location.href='?w=home/user_info';}else{location.href='?w=home/user_info';}</script>";die;
        }
        
    }

    public function actionFindone()
    {
        // print_r(Yii::$app->request->get());die;
        $info = new Advertiser_info();
        $id = Yii::$app->request->get('account_id') ? Yii::$app->request->get('account_id') : 0;
        if ($id) {
            $res['msg'] = $info->getOne($id);
            $res['error'] = true;
        }else{
            $res['msg'] = '数据错误..';
            $res['error'] = false;
        }
        return json_encode($res);
    }

    //用户报名
    public function actionConfirm()
    {
        $model = new UserPosition();
        $id = Yii::$app->request->get('position_id') ? Yii::$app->request->get('position_id') : 0;
        if ($id) {
            $session = new Session();
            $model->position_id = $id;
            $model->user_id = $session['user']['user_id'];
            if ($model->save()) {
                $res['error'] = true;
                $res['msg'] = "报名成功";
            }else{
                $res['error'] = false;
                $res['msg'] = "报名失败";
            }
            return json_encode($res);
        }
    }
}
